package main

import (
	"fmt"
	"strings"
)

func main() {
	var a = "YZY测试"
	fmt.Println(a[0], a[len(a)-1])
	fmt.Println(a[0:len(a)], a[:])
	fmt.Println(&a) // &header
	// fmt.Println(&a[0]) // 错误 不允许这么干

	for i := 0; i < len(a); i++ {
		fmt.Printf("%d: %T %[2]d, %[2]c, %[2]x\n", i, a[i])
	}
	for i, v := range a {
		fmt.Printf("%d: %T %[2]d, %[2]c, %[2]x %v %[3]c \n", i, a[i], v)
	}

	fmt.Println("~~~~~~~~~~~~~~~~~~~~~~~~~")

	var x = "YZY" + "大人"
	fmt.Println(x)
	s1 := fmt.Sprintf("%s%s ---%s", "Y", "Z", "Y大人") // 推荐
	fmt.Println(s1)
	s2 := strings.Join([]string{"YZ", "Y", "测试"}, "*")
	fmt.Println(s2)
	fmt.Println("~~~~~~~~~~~~~~~~~~~~~~~~~")

	// 多次拼接builder
	var builder = strings.Builder{} // 零值
	builder.Write([]byte{97, 0x41, 0x32, 0x20, 0x0a, 0x30})
	builder.WriteByte(98)
	builder.WriteRune(27979)
	builder.WriteString("xyz")
	fmt.Println(builder.String())
	fmt.Println("~~~~~~~~~~~~~~~~~~~~~~~~~")
	fmt.Println('测', "测"[0])
	fmt.Println("~~~~~~~~~~~~~~~~~~~~~~~~~")

	fmt.Println(
		strings.Index(a, "Y"),     // 返回的是索引 index
		strings.LastIndex(a, "Y"), //反方向找索引
		strings.IndexAny(a, "测Y"),
		strings.IndexByte(a, 98),
		strings.IndexRune(a, 27979),
		strings.Count(a, "Y"),
		strings.Contains(a, "ZY"),
		strings.ToLower(a),
		strings.ToUpper(a),
		strings.HasPrefix(a, "Y"),
		strings.HasSuffix(a, "测试"),
	)

	var b = "aabccdefgcg"                // trim 两端移除，不管中间
	fmt.Println(strings.Trim(b, "abgc")) // set 去重的
	fmt.Println(strings.TrimLeft(b, "abgc"))
	fmt.Println(strings.TrimRight(b, "abgc"))
	fmt.Println(strings.TrimSpace(b))

	fmt.Println("~~~~~~~~~~~~~~~~~~~~~~")
	c := "\r\n\t\f abc \r\r\n\t def \t\n\r"
	fmt.Println(strings.TrimSpace(c))
	fmt.Println(strings.TrimPrefix(a, "www."))
	fmt.Println(strings.TrimSuffix(a, "www."))

	fmt.Println("~~~~~~~~~~~~~~~~~~~~~~")

	var d = "www.Yzy.com测试"
	fmt.Println(strings.Split(d, "."))
	fmt.Println(strings.SplitAfter(d, "."))
	fmt.Println(strings.Split(d, "-"))
	fmt.Println(strings.Split(d, ""))
	fmt.Println(strings.SplitN(d, ".", 1)) // n 切片长度，元素个数, n-1 刀
	fmt.Println(strings.SplitN(d, ".", 2)) // n 切片长度，元素个数, n-1 刀
	fmt.Println(strings.SplitN(d, ".", 0)) // n 切片长度，元素个数, n-1 刀

	fmt.Println("~~~~~~~~~~~~~~~~~~~~~~")
	fmt.Println(strings.Cut(d, ".")) // 返回三个值 断点前部分、断点后部分，成功
	fmt.Println(strings.Cut(d, "=")) // 返回 3 个值，a 、 "", false
	// if x, y, ok := strings.Cut(a, "="); ok {
	// }
	fmt.Println(strings.Replace(a, "Y", "A", 10))
	fmt.Println(strings.Repeat("ab", 30))
	fmt.Println("~~~~~~~~~~~~~~~~~~~~~~")

	newstr := strings.Map(func(r rune) rune {
		fmt.Printf("%T, %[1]d, %[1]c, %[1]x, %[1]q \n", r)
		if 'a' <= r && r <= 'z' {
			return r - 32
		}
		return r
	}, d)
	fmt.Println(newstr)
}
